/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.UserData;
import data.OrganisasiData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.OrganisasiDB;

/**
 *
 * @author Muhammad Dony
 */
public class OrganisasiUserServlet extends HttpServlet {
UserData listUser;
    OrganisasiDB organisasidb;
    HttpSession session;
    private String action;
    OrganisasiData listOrganisasi;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    
    
    
    protected void halamanOrganisasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        UserData user = (UserData) session.getAttribute("userMasuk");
        List listorganisasi = new OrganisasiDB().getorganisasiById(user.getID());
        request.setAttribute("listorganisasi", listorganisasi);        
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/view_organisasi.jsp");
        rd.forward(request, response);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // login validation start
        session = request.getSession();
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                action = request.getParameter("action");
                if (request.getParameter("action") == null) {
                    try {
                        halamanOrganisasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("add")) {
                    halamanTambahOrganisasi(request, response);
                } else if (action.equals("edit")) {
                    try {
                        halamanEditOrganisasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("delete")) {
                    try {
                        deleteOrganisasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
                rd.forward(request, response);
            }
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
            rd.forward(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get session
        session = request.getSession();
        // mengambil object sesiUser dari session
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        // mengambil object dengan parameter action dari request
        action = request.getParameter("action");

        if (request.getParameter("action") == null) {
                    try {
                        halamanOrganisasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Tambah")) {
                    try {
                        tambahOrganisasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Edit")) {
                    try {
                        editOrganisasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(OrganisasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                
                }
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public void halamanTambahOrganisasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("title", "Tambah Organisasi");
        request.setAttribute("buttonName", "Tambah");
        request.setAttribute("listOrganisasi", new OrganisasiData());
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/add_organisasi.jsp");
        rd.forward(request, response);
    }

    public void halamanEditOrganisasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Edit Organisasi");
        request.setAttribute("buttonName", "Edit");
        String ID  = request.getParameter("id");
        int no  = Integer.parseInt(request.getParameter("no"));
        List listorganisasi = new OrganisasiDB().getorganisasiByxx(ID, no);
        OrganisasiData dataOrganisasi = (OrganisasiData) listorganisasi.get(0);
        request.setAttribute("dataOrganisasi", dataOrganisasi);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/add_organisasi.jsp");
        rd.forward(request, response);
    }

    public void tambahOrganisasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listOrganisasi = new OrganisasiData();
        // set listUser
        listOrganisasi.setID(request.getParameter("ID"));
        listOrganisasi.setOrganisasi(request.getParameter("organisasi"));
        listOrganisasi.setJabatan(request.getParameter("jabatan"));
        listOrganisasi.setAwal(request.getParameter("awal"));
        listOrganisasi.setAkhir(request.getParameter("akhir"));
        // memasukkannya dalam database
        organisasidb = new OrganisasiDB();
        organisasidb.insertOrganisasi(listOrganisasi);
        // redirect ke halaman pelaporan
        halamanOrganisasi(request, response);
    }

    public void editOrganisasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listOrganisasi = new OrganisasiData();
        // set listUser
        listOrganisasi.setNomor(Integer.parseInt(request.getParameter("no")));
        listOrganisasi.setID(request.getParameter("ID"));
        listOrganisasi.setOrganisasi(request.getParameter("organisasi"));
        listOrganisasi.setJabatan(request.getParameter("jabatan"));
        listOrganisasi.setAwal(request.getParameter("awal"));
        listOrganisasi.setAkhir(request.getParameter("akhir"));
        // memasukkannya dalam database
        organisasidb = new OrganisasiDB();
        organisasidb.updateOrganisasi(listOrganisasi);
        // redirect ke halaman pelaporan
        halamanOrganisasi(request, response);
    }

    
    //ambilnya dari url di bagian view
    public void deleteOrganisasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        String ID = request.getParameter("id");
        int org = Integer.parseInt(request.getParameter("organisasi"));
        organisasidb = new OrganisasiDB();
        organisasidb.deleteOrganisasi(ID, org);
        halamanOrganisasi(request, response);
    }
    
}
